package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-03-11
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        // 构建查询条件
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        // 判断前端输入的状态
        if (dto.getStatus() != null) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        // 分页查询
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<ApUserRealname> iPage = this.page(page, query);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),
                iPage.getTotal(), iPage.getRecords());
        return result;
    }

    @Autowired
    private IApUserService userService;

    /**
     * 认证审核
     *
     * @param dto
     * @param type 1: 通过  0: 拒绝
     * @return
     */
    @Override
    public ResponseResult auth(AuthDto dto, int type) {
        // 根据id查询认证记录
        ApUserRealname apUserRealname = this.getById(dto.getId());
        // 审核失败 修改认证表的状态为2  并且记录失败的原因
        if (type == 0) {
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            this.updateById(apUserRealname);
        } else {
            // 审核成功
            // 查询用户注册表信息
            ApUser apUser = userService.getById(apUserRealname.getUserId());
            // 创建自媒体账号
            WmUser wmUser = createWmUser(apUser);
            if (wmUser != null) {
                // 创建文章作者
                ApAuthor apAuthor = createAuthor(apUser, wmUser.getId());
                if (apAuthor != null) {
                    // 将作者的id更新到自媒体账号表中
                    wmUser.setApAuthorId(apAuthor.getId());
                    mediaFeign.updateWmUser(wmUser);
                }
                // 修改认证表的状态为9
                apUserRealname.setStatus(9);
                this.updateById(apUserRealname);
                // 修改用户注册表的标识flag为1 -自媒体人
                apUser.setFlag(1);
                userService.updateById(apUser);
            }
        }
        return ResponseResult.okResult();
    }

    /**
     * 创建作者
     *
     * @param apUser
     * @param wmUserId
     * @return
     */
    private ApAuthor createAuthor(ApUser apUser, Integer wmUserId) {
        // 远程调用文章服务创建作者
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setType(2);
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUserId);
        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.saveApAuthor(author);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = apAuthorResponseResult.getData();
            return apAuthor;
        }
        return null;
    }

    @Autowired
    private MediaFeign mediaFeign;

    @Autowired
    private ArticleFeign articleFeign;

    /**
     * 创建自媒体用户
     *
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        // 远程调用自媒体服务创建账号
        WmUser wmUser = new WmUser();
        // 从apUser 中拷贝相同的属性  只有类型相同,字段名称相同这里才会自动赋值
        BeanUtils.copyProperties(apUser, wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        ResponseResult<WmUser> wmUserResponseResult = mediaFeign.saveWmUser(wmUser);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser user = wmUserResponseResult.getData();
            return user;
        }
        return null;
    }
}
